package com.allen.imsystem.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.moderation.v2.ModerationClient;
import com.huaweicloud.sdk.moderation.v2.model.*;
import com.huaweicloud.sdk.moderation.v2.region.ModerationRegion;
import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @ClassName ModerationUtil
 * @Description
 * @Version 1.0
 */
public class ModerationUtil {
    private static final String AK = "PSZCVD2V8TZHX5WQ0LG1";
    private static final String SK = "yuAxOPx9SkNJjXNZffCpTHAtifxBoDbnlCozHEdH";

    private static ModerationClient client;

    static {
        ICredential auth = new BasicCredentials()
                .withAk(AK)
                .withSk(SK);
        client = ModerationClient.newBuilder()
                .withCredential(auth)
                .withRegion(ModerationRegion.valueOf("cn-east-3"))
                .build();
    }

    public static String CheckText(String text) {
        RunTextModerationRequest request = new RunTextModerationRequest();
        TextDetectionReq body = new TextDetectionReq();
        List<TextDetectionItemsReq> listbodyItems = new ArrayList<>();
        listbodyItems.add(
                new TextDetectionItemsReq()
                        .withText(text)
                        .withType("content")
        );
        List<String> listbodyCategories = new ArrayList<>();
        listbodyCategories.add("politics");
        listbodyCategories.add("abuse");
        listbodyCategories.add("porn");
        body.withItems(listbodyItems);
        body.withCategories(listbodyCategories);
        request.withBody(body);
        try {
            RunTextModerationResponse response = client.runTextModeration(request);
            TextDetectionResponseResult result = response.getResult();

            System.out.println(JSON.toJSON(response));

            if ("block".equals(result.getSuggestion())) {
                LinkedHashMap<String, List<String>> lhm =
                        (LinkedHashMap<String, List<String>>) response.getResult().getDetail();
                StringBuilder regex = new StringBuilder();
                for (Map.Entry<String, List<String>> entry : lhm.entrySet()) {
                    for (String keyword : entry.getValue()) {
                        regex.append(keyword).append("|");
                    }
                }
                if (regex.length() > 0) {
                    regex.deleteCharAt(regex.length() - 1);
                }
                return text.replaceAll(regex.toString(), "***");
            } else {
                return text;
            }

        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return text;
    }

    public static boolean CheckImage(String url, byte[] image) {
        RunImageModerationRequest request = new RunImageModerationRequest();
        ImageDetectionReq body = new ImageDetectionReq();
        List<ImageDetectionReq.CategoriesEnum> listbodyCategories = new ArrayList<>();
        listbodyCategories.add(ImageDetectionReq.CategoriesEnum.fromValue("politics"));
        listbodyCategories.add(ImageDetectionReq.CategoriesEnum.fromValue("terrorism"));
        listbodyCategories.add(ImageDetectionReq.CategoriesEnum.fromValue("porn"));
        body.withCategories(listbodyCategories);
        body.withModerationRule("default");
//        body.withUrl(url);
        body.withImage(image);
        request.withBody(body);
        try {
            RunImageModerationResponse response = client.runImageModeration(request);
            System.out.println(response.getHttpStatusCode());
            System.out.println(JSON.toJSONString(response));
            return "block".equals(response.getResult().getSuggestion());

        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return false;
    }

    public static void main(String[] args) {
        boolean b = CheckImage(null, null);
        System.out.println(b);
    }
}
